package commons.drawing;

import commons.ihm.ParametresAffichageCarto;
import commons.ihm.ParametresAffichageCarto.TypeAffichage;
import commons.utils.UtilsGeom;
import commons.utils.primitive_geom.Point_d;
import commons.utils.primitive_geom.Segment_bound;
import commons.utils.primitive_geom.Segment_d;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

// Implémente une partie de l'interface I_Drawable pour des segments
public class SegmentDrawable extends Segment_bound implements I_Drawable {

	public Color color;
	public Color getColor() {return color;}

	// Une largeur réelle dépendant de l'échelle, pour le mode PAPIER
	protected float width_reel_m;
	// Une alrgeur fixe en pixel, pour le mode TOPO
	protected float width_topo_px;


	public SegmentDrawable(Color color, float width_reel_m, float width_topo_px) {
		super(0,0,0,0);
		this.color = color;
		this.width_reel_m = width_reel_m;
		this.width_topo_px = width_topo_px;
	}

	public SegmentDrawable(Segment_d seg, Color color, float width_reel_m, float width_topo_px) {
		super(seg);
		this.color = color;
		this.width_reel_m = width_reel_m;
		this.width_topo_px = width_topo_px;
	}

	public SegmentDrawable(Point_d pt1, Point_d pt2, Color color, float width_reel_m, float width_topo_px) {
		super(pt1, pt2);
		this.color = color;
		this.width_reel_m = width_reel_m;
		this.width_topo_px = width_topo_px;
	}

	// largeur fixe, nouveaux points
	public SegmentDrawable(Color color, float width_topo_px) {
		super(0,0,0,0);
		this.color = color;
		this.width_reel_m = -1;
		this.width_topo_px = width_topo_px;
	}
	//largeur fixe, lien vers points existants
	public SegmentDrawable(Point_d pt1, Point_d pt2, Color color, float width_topo_px) {
		super(pt1, pt2);
		this.color = color;
		this.width_reel_m = -1;
		this.width_topo_px = width_topo_px;
	}

	Line2D.Float shLine = new Line2D.Float();
	@Override
	public void draw(Graphics2D g, ParametresAffichageCarto pac) {

		shLine.x1 = pac.x2X(pt1.x);
		shLine.y1 = pac.y2Y(pt1.y);
	    shLine.x2 = pac.x2X(pt2.x);
		shLine.y2 = pac.y2Y(pt2.y);

		// Cas spécial des segment à largeur fixe quel que soit le mode d'affichage
		if (width_reel_m<0)
			g.setStroke(pac.getBasicStroke(width_topo_px));

		// Sinon, la largeur dépend du mode d'affichage
		else if(pac.typeAffichage==TypeAffichage.PAPIER) {
			g.setStroke(pac.getBasicStroke(pac.lg2LG(width_reel_m)));
		} else if (pac.typeAffichage == TypeAffichage.TOPO) {
			g.setStroke(pac.getBasicStroke(width_topo_px));
		} else {
			assert false : "Type d'affichage inconnu : " + pac.typeAffichage;
		}
		
		g.setColor(pac.getColorSelonModeAffichage(getColor()));

		g.draw (shLine);
	}

	@Override
	public double distTo(Point_d pt) {
		return UtilsGeom.getDistancePtSegment(pt1, pt2, pt);
	}

	@Override
	public String getInfoBulle() {
		return super.toString();
	}

	@Override
	public String getInfoDetaillee() {
		return super.toString();
	}


}
